<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>基础数据选择宏控件</title>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
    <!--[if lte IE 6]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-ie6.css">
    <![endif]-->
    <!--[if lte IE 7]>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/ie.css">
    <![endif]-->
    <link rel="stylesheet" href="leipi.style.css">
    <script src="../../../../jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../dialogs/internal.js"></script>
    <link rel="stylesheet" type="text/css" href="../../../../select2/select2.css" />
    <script src="../../../../select2/select2.js"></script>
    <script type="text/javascript">
        /* Thank you by
        http://www.alt-tag.com/blog/2006/02/ie-dom-bugs/ */
        function createElement(type, name) {
            var element = null;
            try {
                element = document.createElement('<' + type + ' name="' + name + '">');
            } catch (e) {
            }
            if (element == null) {
                element = document.createElement(type);
                element.name = name;
            }
            return element;
        }
    </script>
</head>
<body>
<div class="content">
    <table class="table table-bordered table-striped table-hover">
        <tr>
            <th width="50%">基础数据选择宏控件&nbsp;字段&nbsp;<span class="label label-important">*</span></th>
            <th><span>控件名称&nbsp;</span><span class="label label-important">*</span></th>
        </tr>
        <tr>
            <th><input type="text" id="orgname" placeholder="必填项"></th>
            <th><input type="text" id="orgtitle" placeholder="必填项"></th>
        </tr>
        <tr>
            <th><span>默认值</span></th>
            <th>必填项</th>
        </tr>
        <tr>
            <td><input type="text" id="orgvalue" placeholder="无则不填"></td>
            <td>
                <input id="canNull" name="canNull" type="checkbox" value="0"/>
            </td>
        </tr>
        <tr>
            <th><span>数据类型</span></th>
            <th><span>长度/大小</span></th>
        </tr>
        <tr>
            <td>
                <select id="orgtype" onchange="orgtypeOnChange()">
                    <option value="0">字符串型</option>
                    <option value="1">文本型</option>
                    <option value="2">整型</option>
                    <option value="3">长整型</option>
                    <option value="4">布尔型</option>
                    <option value="5">浮点型</option>
                    <option value="6">双精度型</option><!--
                <option value="7">日期型</option>
                <option value="8">日期时间型</option>
                -->
                    <option value="9">价格型</option>
                </select>
                <script>
                    function orgtypeOnChange() {
                        if (!oNode) {
                            if ($G('orgtype').value >= 2) {
                                $G('maxV').value = '';
                            } else {
                                $G('maxV').value = '100';
                            }
                        }
                    }
                </script>
            </td>
            <td>
                <select id="minT" name="minT" style="width:60px">
                    <option value="d=">>=</option>
                    <option value="d">></option>
                    <option value="=">=</option>
                </select>
                <input id="minV" name="minV" type="text" style="width:40px">
                <select id="maxT" name="maxT" style="width:60px">
                    <option value="x="><=</option>
                    <option value="x"><</option>
                </select>
                <input id="maxV" name="maxV" type="text" value="100" style="width:40px">

                <select id="orgalign" style="display:none">
                    <option value="left">左对齐</option>
                    <option value="center">居中对齐</option>
                    <option value="right">右对齐</option>
                </select>
            </td>
        </tr>
        <tr>
            <th>
                <span>
                    宽
                    <span style="display: none">&nbsp;&nbsp;&nbsp;&&nbsp;&nbsp;&nbsp;字体大小</span>
                </span>
            </th>
            <th><span>提示</span><span style="display: none">可见性</span></th>
        </tr>
        <tr>
            <td>
                <input id="orgwidth" type="text" value="150" class="input-small span1" placeholder="auto"/>
                <select id="unit" style="width:60px">
                    <option value="px" selected>px</option>
                    <option value="%">%</option>
                </select>
                <span style="display: none">X
                <input id="orgheight" type="text" value="" class="input-small span1" placeholder="auto"/>
                &
                <input id="orgfontsize" type="text" value="" class="input-small span1" placeholder="auto"/> px
                </span>
            </td>
            <td>
                <input type="text" id="tip" name="tip"/>
                <label class="checkbox inline" style="display:none"><input id="orghide" name="orghide" type="checkbox"/>隐藏</label>
            </td>
        </tr>
        <tr>
            <th><span>只读</span></th>
            <th><span>只读类型</span></th>
        </tr>
        <tr>
            <td>
                <input id="isReadOnly" name="isReadOnly" type="checkbox" value="1"/>
            </td>
            <td>
                <select id="readOnlyType">
                    <option value="">不限</option>
                    <option value="0">仅添加时</option>
                    <option value="1">仅编辑时</option>
                    <option value="2">仅编辑引用记录时</option>
                </select>
                <br/>仅编辑引用记录时适用于”嵌套表格2“宏控件选取的记录
            </td>
        </tr>
        <tr>
            <th><span>基础数据</span></th>
            <th><span>请求参数</span></span></th>
        </tr>
        <tr>
            <td>
                <select id="sel"></select>
            </td>
            <td>
                <input id="requestParam" name="requestParam" type="text" title="request请求参数名称"/><br/>(默认为字段名)
            </td>
        </tr>
        <tr id="trLayerTitle">
            <th><span id="spanLayer">可选层级</th>
            <th><span></span></th>
        </tr>
        <tr id="trLayer">
            <td>
                <span id="layerBox">
                    <select id="layer" name="layer" title="设为1级时，只显示第1层级，设为末级时，只能选择末级">
                        <option value="">不限</option>
                        <option value="1">1级</option>
                        <option value="0">末级</option>
                    </select>
                    <br/>（仅对树形基础数据有效）
                </span>
            </td>
            <td>
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    var oNode = null, thePlugins = 'macros';

    var formCode = '', macroType = '';
    var href = window.location.href;
    var p = href.indexOf('formCode=');
    if (p!=-1) {
        var q = href.indexOf('&', p);
        if (q == -1) {
            formCode = href.substring(p + 'formCode='.length);
        } else {
            formCode = href.substring(p, q);
        }
    }

    var p = href.indexOf('macroType=');
    if (p!=-1) {
        var q = href.indexOf('&', p);
        if (q == -1) {
            macroType = href.substring(p + 'macroType='.length);
        } else {
            macroType = href.substring(p + 'macroType='.length, q);
        }
    }

    var isTree = macroType == 'macro_basic_tree_select_ctl';
    console.log('href', href, 'formCode', formCode, 'macroType', macroType, 'isTree', isTree);
    if (!isTree) {
        $('#trLayerTitle').hide();
        $('#trLayer').hide();
    }

    async function initFields() {
        var params = {isTree: isTree};
        await window.parent.ajaxPost('/basicdata/listByType', params).then((res) => {
            var data = res.data;
            var opts = '<option value="">无</option>';
            for (var i in data) {
                opts += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
            }
            $('#sel').append(opts);
        });
    }

    window.onload = function () {
        var desc = '';

        if (UE.plugins[thePlugins].editdom) {
            oNode = UE.plugins[thePlugins].editdom;

            var gValue = oNode.getAttribute('macroDefaultValue');
            if (gValue)
                gValue = gValue.replace(/&quot;/g, "\"");
            var gName = oNode.getAttribute('name').replace(/&quot;/g, "\"");
            var gTitle = oNode.getAttribute('title').replace(/&quot;/g, "\"");
            var gHidden = oNode.getAttribute('orghide');
            var gFontSize = oNode.getAttribute('orgfontsize');
            var gAlign = oNode.getAttribute('orgalign');
            var gWidth = oNode.getAttribute('orgwidth');
            var gHeight = oNode.getAttribute('orgheight');

            var gType = oNode.getAttribute('fieldType');
            var gCanNull = oNode.getAttribute("canNull");
            var gMinT = oNode.getAttribute("minT");
            var gMinV = oNode.getAttribute("minV");
            var gMaxT = oNode.getAttribute("maxT");
            var gMaxV = oNode.getAttribute("maxV");
            var gTip = oNode.getAttribute("tip");
            if (gTip == null) {
                gTip = '';
            }

            var isReadOnly = oNode.getAttribute("readonly");
            if (isReadOnly != null) {
                $G('isReadOnly').checked = true;
            } else {
                $G('isReadOnly').checked = false;
            }

            var gReadOnlyType = oNode.getAttribute("readOnlyType");
            if (gReadOnlyType == null) {
                gReadOnlyType = '';
            }
            $G('readOnlyType').value = gReadOnlyType;

            gValue = gValue == null ? '' : gValue;
            gTitle = gTitle == null ? '' : gTitle;

            $G('orgvalue').value = gValue;
            $G('orgname').value = gName;
            $G('orgtitle').value = gTitle;
            if (gHidden == '1') {
                $G('orghide').checked = true;
            }
            $G('orgfontsize').value = gFontSize;
            $G('orgwidth').value = gWidth;
            var gUnit = 'px';
            if (gWidth.endsWith('%')) {
                gUnit = '%';
                gWidth = gWidth.substring(0, gWidth.length - 1);
            } else if (gWidth.endsWith('px')) {
                gWidth = gWidth.substring(0, gWidth.length - 2);
            }
            $G('unit').value = gUnit;

            $G('orgheight').value = gHeight;
            $G('orgalign').value = gAlign;
            $G('orgtype').value = gType;

            $G('orgname').setAttribute("readonly", true);
            $G('orgtype').setAttribute("disabled", true);
            if (gCanNull == 0) {
                $G('canNull').checked = true;
            } else {
                $G('canNull').checked = false;
            }

            if (gMinT) {
                $G('minT').value = gMinT;
                $G('minV').value = gMinV;
                $G('maxT').value = gMaxT;
                $G('maxV').value = gMaxV;
            }
            $G('tip').value = gTip;

            desc = oNode.getAttribute('description');
        }

        initFields().then(()=> {
            var val = desc;
            if (desc.indexOf('{')==0) {
                var json = $.parseJSON(desc);
                console.log('initFields json', json);
                val = json.code;

                $('#layer').val(json.layer);
                $('#requestParam').val(json.requestParam);
            }
            $('#sel').val(val);
            $('#sel').select2();
        })
    }
    dialog.oncancel = function () {
        if (UE.plugins[thePlugins].editdom) {
            delete UE.plugins[thePlugins].editdom;
        }
    };
    dialog.onok = function () {
        var gName = $G('orgname').value.replace(/\"/g, "&quot;");
        if (gName == '') {
            alert('请输入控件字段');
            return false;
        }
        // var gTitle=$G('orgtitle').value.replace(/\"/g,"&quot;");
        var gTitle = $G('orgtitle').value;
        if (gTitle == '') {
            alert('请输入控件名称');
            return false;
        }

        // 不需要转义
        // var gValue=$G('orgvalue').value.replace(/\"/g,"&quot;");
        var gValue = $G('orgvalue').value;
        var gFontSize = $G('orgfontsize').value;
        var gAlign = $G('orgalign').value;
        var gWidth = $G('orgwidth').value;
        var gUnit = $G('unit').value;
        var gHeight = $G('orgheight').value;

        var gType = $G('orgtype').value;
        var gCanNull = $G('canNull').checked ? 0 : 1;
        var gMinT = $G('minT').value;
        var gMinV = $G('minV').value;
        var gMaxT = $G('maxT').value;
        var gMaxV = $G('maxV').value;
        var gTip = $G('tip').value;
        var gReadOnlyType = $G('readOnlyType').value;

        // 字符串型字段的字数最大为21845，相当于：最大长度65532字节/3
        // 此处将边界值设为20000，是考虑到还有其它字段，如果一个表中的字段总长度超长会报错，表一行最多支持65535字符(不包括text等)
        if (gType == 0) {
            if (gMinV >= 20000) {
                alert('最小长度不能超过20000');
                return false;
            }
            if (gMinV != '' && gMaxV != '') {
                if (parseInt(gMinV) > parseInt(gMaxV)) {
                    alert('最小长度不能大于最大长度');
                    return false;
                }
            }

            if (gMaxV == '') {
                alert('请输入最大长度');
                return false;
            } else if (gMaxV > 20000) {
                alert('最大长度不能超过20000');
                return false;
            }
        }

        if ($('#sel').val() == '') {
            alert('请选择基础数据');
            return false;
        }
        var json = {};
        json.code = $('#sel').val();
        json.layer = $('#layer').val();
        json.requestParam = $('#requestParam').val();
        var desc = JSON.stringify(json);
        console.log('desc', desc);

        if (!oNode) {
            try {
                oNode = createElement('input', gName);
                oNode.setAttribute('type', 'text');
                oNode.setAttribute('title', gTitle);
                oNode.setAttribute('macroDefaultValue', gValue);

                oNode.setAttribute('cwsPlugins', thePlugins);
                if ($G('orghide').checked) {
                    oNode.setAttribute('orghide', 1);
                } else {
                    oNode.setAttribute('orghide', 0);
                }
                if (gFontSize != '') {
                    oNode.style.fontSize = gFontSize + 'px';
                    //style += 'font-size:' + gFontSize + 'px;';
                    oNode.setAttribute('orgfontsize', gFontSize);
                }
                if (gAlign != '') {
                    //style += 'text-align:' + gAlign + ';';
                    oNode.style.textAlign = gAlign;
                    oNode.setAttribute('orgalign', gAlign);
                }
                if (gWidth != '') {
                    oNode.style.width = gWidth + gUnit;
                    oNode.setAttribute('orgwidth', gWidth + gUnit);
                }
                else {
                    oNode.style.width = '';
                    oNode.setAttribute('orgwidth', '');
                }
                if (gHeight != '') {
                    oNode.style.height = gHeight + 'px';
                    // style += 'height:' + gHeight + 'px;';
                    oNode.setAttribute('orgheight', gHeight);
                }

                if (gType != '') {
                    oNode.setAttribute('fieldType', gType);
                }

                oNode.setAttribute("canNull", gCanNull);
                oNode.setAttribute("minT", gMinT);
                oNode.setAttribute("minV", gMinV);
                oNode.setAttribute("maxT", gMaxT);
                oNode.setAttribute("maxV", gMaxV);

                if ($G('isReadOnly').checked) {
                    oNode.setAttribute("readonly", "readonly");
                } else {
                    oNode.removeAttribute("readonly");
                }

                oNode.setAttribute("tip", gTip);
                oNode.setAttribute("readOnlyType", gReadOnlyType);
                // oNode.setAttribute('style',style );
                // oNode.style.cssText=style;//ie7

                oNode.setAttribute("description", desc);
                oNode.setAttribute("macroType", macroType);
                if (macroType == 'macro_basic_tree_select_ctl') {
                    oNode.setAttribute("value", "宏控件：树形基础数据选择框");
                } else {
                    oNode.setAttribute("value", "宏控件：基础数据选择");
                }
                oNode.setAttribute('kind', 'macro');

                editor.execCommand('insertHtml', oNode.outerHTML);
            } catch (e) {
                console.log(e);
                try {
                    editor.execCommand('error');
                } catch (e) {
                    alert('控件异常！');
                }
                return false;
            }
        } else {
            oNode.setAttribute('title', gTitle);
            oNode.setAttribute('macroDefaultValue', gValue);
            if ($G('orghide').checked) {
                oNode.setAttribute('orghide', 1);
            } else {
                oNode.setAttribute('orghide', 0);
            }
            if (gFontSize != '') {
                //oNode.style.fontSize = gFontSize+ 'px';
                oNode.setAttribute('orgfontsize', gFontSize);
            } else {
                //oNode.style.fontSize = '';
                oNode.setAttribute('orgfontsize', '');
            }
            if (gAlign != '') {
                //oNode.style.textAlign = gAlign;
                oNode.setAttribute('orgalign', gAlign);
            } else {
                oNode.setAttribute('orgalign', '');
            }
            if (gWidth != '') {
                oNode.style.width = gWidth + 'px';
                oNode.setAttribute('orgwidth', gWidth + gUnit);
            } else {
                oNode.style.width = '';
                oNode.setAttribute('orgwidth', '');
            }
            if (gHeight != '') {
                oNode.style.height = gHeight + 'px';
                oNode.setAttribute('orgheight', gHeight);
            } else {
                oNode.style.height = '';
                oNode.setAttribute('orgheight', '');
            }

            /*
            if( gType != '' ) {
                oNode.setAttribute('fieldType',gType );
            }else{
                oNode.setAttribute('fieldType', '');
            }
            */

            oNode.setAttribute("canNull", gCanNull);
            oNode.setAttribute("minT", gMinT);
            oNode.setAttribute("minV", gMinV);
            oNode.setAttribute("maxT", gMaxT);
            oNode.setAttribute("maxV", gMaxV);

            if ($G('isReadOnly').checked) {
                oNode.setAttribute("readonly", "readonly");
            } else {
                oNode.removeAttribute("readonly");
            }

            oNode.setAttribute("tip", gTip);
            oNode.setAttribute("readOnlyType", gReadOnlyType);

            oNode.setAttribute("description", desc);
            oNode.setAttribute("macroType", macroType);
            oNode.setAttribute('kind', 'macro');

            delete UE.plugins[thePlugins].editdom;
        }
    };
</script>
</body>
</html>